<template>
    <view class="comment-container" :class="{ 'show': show }">
        <!-- 顶部标题栏 -->
        <view class="comment-header">
            <text class="title">{{ totalComments }}条评论</text>
            <text class="close-btn" @tap="handleClose">×</text>
        </view>

        <!-- 评论列表 -->
        <scroll-view class="comment-list" scroll-y>
            <view class="comment-item" v-for="(item, index) in commentList" :key="index">
                <view class="user-info">
                    <image class="avatar" :src="item.avatar" mode="aspectFill"></image>
                    <view class="info">
                        <text class="username">{{ item.username }}</text>
                        <text class="time">{{ item.time }}</text>
                    </view>
                </view>
                <view class="comment-content">{{ item.content }}</view>
                <view class="comment-actions">
                    <view class="action-item" @tap="handleReply(item)">
                        <text class="action-text">回复</text>
                    </view>
                    <view class="action-item" @tap="handleLike(item)">
                        <text class="like-count">{{ item.likes }}</text>
                        <text class="action-text">赞</text>
                    </view>
                </view>
                <!-- 回复列表 -->
                <view class="reply-list" v-if="item.replies && item.replies.length > 0">
                    <view class="reply-item" v-for="(reply, replyIndex) in item.replies" :key="replyIndex">
                        <text class="reply-username">{{ reply.username }}</text>
                        <text class="reply-content">:{{ reply.content }}</text>
                    </view>
                </view>
            </view>
        </scroll-view>

        <!-- 底部评论输入框 -->
        <view class="comment-input">
            <input type="text" v-model="commentText" placeholder="说点什么..." :focus="inputFocus"
                @blur="inputFocus = false" />
            <button class="send-btn" @tap="sendComment">发送</button>
        </view>
    </view>
</template>

<script>
export default {
    props: {
        show: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            totalComments: 137533,
            commentText: '',
            inputFocus: false,
            commentList: [
                {
                    id: 1,
                    username: '古苏',
                    avatar: '../static/img/album.png',
                    content: '雷总，我们一般早上吃苦',
                    time: '11小时前·河北',
                    likes: '19.7万',
                    replies: []
                },
                {
                    id: 2,
                    username: '尼古拉斯',
                    avatar: '../static/img/album.png',
                    content: '只有早上吃吗？中午晚上不吃吗？😂',
                    time: '9小时前·山东',
                    likes: '2.4万',
                    replies: []
                },
                {
                    id: 3,
                    username: '醒醒成精记',
                    avatar: '../static/img/album.png',
                    content: '中午吃亏，下午吃瘪，晚上吃饼😭',
                    time: '9小时前·广东',
                    likes: '2.9万',
                    replies: []
                }
            ]
        }
    },
    methods: {
        handleClose() {
            this.$emit('close');
        },
        handleReply(item) {
            this.inputFocus = true;
            this.commentText = `回复 @${item.username}：`;
        },
        handleLike(item) {
            // 处理点赞逻辑
            uni.showToast({
                title: '点赞成功',
                icon: 'none'
            });
        },
        sendComment() {
            if (!this.commentText.trim()) {
                return;
            }
            // 添加新评论
            this.commentList.unshift({
                id: Date.now(),
                username: '我',
                avatar: '../static/img/album.png',
                content: this.commentText,
                time: '刚刚',
                likes: '0',
                replies: []
            });
            this.commentText = '';
            this.totalComments++;
        }
    }
}
</script>

<style lang="scss">
.comment-container {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 75vh;
    background-color: #fff;
    border-radius: 20rpx 20rpx 0 0;
    transform: translateY(100%);
    transition: transform 0.3s ease-out;
    z-index: 999;

    &.show {
        transform: translateY(0);
    }

    .comment-header {
        padding: 30rpx;
        border-bottom: 1rpx solid #f5f5f5;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;

        .title {
            font-size: 32rpx;
            font-weight: bold;
            color: #333;
        }

        .close-btn {
            position: absolute;
            right: 30rpx;
            font-size: 40rpx;
            color: #999;
            padding: 10rpx;
        }
    }

    .comment-list {
        height: calc(75vh - 180rpx);
        padding: 0 30rpx;

        .comment-item {
            padding: 30rpx 0;
            border-bottom: 1rpx solid #f5f5f5;

            .user-info {
                display: flex;
                align-items: center;
                margin-bottom: 16rpx;

                .avatar {
                    width: 70rpx;
                    height: 70rpx;
                    border-radius: 50%;
                    margin-right: 20rpx;
                }

                .info {
                    flex: 1;

                    .username {
                        font-size: 28rpx;
                        color: #333;
                        font-weight: bold;
                        display: block;
                    }

                    .time {
                        font-size: 24rpx;
                        color: #999;
                    }
                }
            }

            .comment-content {
                font-size: 30rpx;
                color: #333;
                line-height: 1.5;
                margin-bottom: 16rpx;
            }

            .comment-actions {
                display: flex;
                justify-content: flex-end;
                align-items: center;

                .action-item {
                    display: flex;
                    align-items: center;
                    margin-left: 40rpx;

                    .like-count {
                        font-size: 26rpx;
                        color: #999;
                        margin-right: 8rpx;
                    }

                    .action-text {
                        font-size: 26rpx;
                        color: #999;
                    }
                }
            }

            .reply-list {
                margin-top: 20rpx;
                padding: 20rpx;
                background: #f8f8f8;
                border-radius: 10rpx;

                .reply-item {
                    font-size: 28rpx;
                    color: #333;
                    margin-bottom: 10rpx;

                    .reply-username {
                        color: #576b95;
                        font-weight: bold;
                    }
                }
            }
        }
    }

    .comment-input {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 20rpx 30rpx;
        background: #fff;
        border-top: 1rpx solid #f5f5f5;
        display: flex;
        align-items: center;

        input {
            flex: 1;
            height: 72rpx;
            background: #f5f5f5;
            border-radius: 36rpx;
            padding: 0 30rpx;
            font-size: 28rpx;
            margin-right: 20rpx;
        }

        .send-btn {
            width: 120rpx;
            height: 72rpx;
            line-height: 72rpx;
            text-align: center;
            background: #2ed573;
            color: #fff;
            font-size: 28rpx;
            border-radius: 36rpx;
            margin: 0;
        }
    }
}
</style>
